import { defineComponent, reactive, ref, computed } from "vue";
import Page from "@/components/public/page/page";
import { CardWithTitle } from "@/components/public/card/card";
import LTable from "@/components/public/lTable/lTable";
import personalPerformanceService from "@/modules/channelManagement/personalPerformance.service";
import useNavigator from "@/hooks/useNavigator";
import Search from "./popup/search/search";
export default defineComponent({
  setup() {
    const navigator = useNavigator();
    const state = reactive({
      toolbar: {
        left: ["refresh", "output", "close"],
        right: ["filter","search"],
      },
      dataSource: [],
      columns: [
        {
          title: "ID",
          dataIndex: "id",
          align: "center",
          width: "80px",
        },
        {
          title: "订单号",
          dataIndex: "order_no",
          align: "center",
          width: "150px",
        },
        {
          title: "保单号",
          dataIndex: "SecondaryChannels",
          align: "center",
          width: "150px",
        },
        {
          title: "类型",
          dataIndex: "order_type",
          align: "center",
          width: "180px",
        },
        {
          title: "一级渠道",
          dataIndex: ["first_level","label"],
          align: "center",
          width: "220px",
        },
        {
          title: "二级渠道",
          dataIndex: ["second_level","label"],
          align: "center",
          width: "180px",
        },
        {
          title: "三级渠道",
          dataIndex: ["third_level","label"],
          align: "center",
          width: "220px",
        },
        {
          title: "推广员姓名",
          dataIndex: "realname",
          align: "center",
          width: "220px",
        },
        {
          title: "推广人员编码",
          dataIndex: "number",
          align: "center",
          width: "220px",
        },
        {
          title: "投保人姓名",
          dataIndex: "applicant_name",
          align: "center",
          width: "220px",
        },
        {
          title: "投保人证件号",
          dataIndex: "applicant_id_no",
          align: "center",
          width: "220px",
        },
        {
          title: "被保人姓名",
          dataIndex: "insured_name",
          align: "center",
          width: "180px",
        },
        {
          title: "被保人证件号",
          dataIndex: "insured_id_no",
          align: "center",
          width: "220px",
        },
        {
          title: "状态",
          dataIndex: ["order_status","label"],
          align: "center",
          width: "80px",
        },
        {
          title: "保费",
          dataIndex: "sum_premium",
          align: "center",
          width: "80px",
        },
      ],
      query: {
        saler_id: navigator.route?.query?.saler_id || '',
      },
      loading: false,
      searchForm: {
        status: {
          type: "select",
          label: "状态",
          attrs: { placeholder: "请选择状态" },
          datas: [
            { label: "全部", value: 1 },
            { label: "已承包", value: 2 },
            { label: "未承保", value: 3 },
          ],
        }
      },
      pagination: {
        total: 0,
        current: 1,
        pageSize: 10,
      },
      selectedRowKeys: [],
    } as any)
    // 表格节点
    const LTableRef = ref(null as any);
    //服务
    const $service = new personalPerformanceService();
    // 表格状态变更
    const tableChangeHandler = (pag: { pageSize: number; current: number }) => {
      state.pagination.current = pag.current;
      state.pagination.pageSize = pag.pageSize;
    };
    // 选中项变更
    const onSelectChange = (changableRowKeys: any[]) => {
      state.selectedRowKeys = changableRowKeys;
    };
    const rowSelection = computed(() => {
      return {
        selectedRowKeys: state.selectedRowKeys,
        fixed: true,
        onChange: onSelectChange,
      };
    });
    const searchPopupRef = ref(null as any);
    return () => (
      <Page>
        <CardWithTitle title="个人业绩">
        </CardWithTitle>
        <LTable
          ref={LTableRef}
          service={$service}
          toolbar={state.toolbar}
          v-model={state}
        >
          <a-table
            bordered={true}
            scroll={{ x: "max-content" }}
            dataSource={state.dataSource}
            columns={state.columns}
            pagination={state.pagination}
            onChange={tableChangeHandler}
            row-selection={rowSelection.value}
            loading={state.loading}
            rowKey="id"
            v-slots={{
              bodyCell: ({ column, record }: any) => {
                //多个被保人
                if (column.dataIndex === "insured_name") {
                  return !record.insured_name &&(
                    <div class="operation">
                      <a-button
                        type="primary"
                        size="small"
                        onClick={() => {
                          searchPopupRef.value.open({
                            id: record.id,
                          })
                        }}
                      >
                        查看
                      </a-button>
                    </div>
                  )
                }
              }
            }}
          />
        </LTable>
        <Search 
          ref={searchPopupRef}
        />
      </Page>
    )
  }
})